﻿@model S4Store.Models.AfterSaleReport.AfterSaleReportServiceAdvisorModel
@{
    ViewBag.Title = "ZKTS";
    Layout = null;
}
@{
    if (Model.StandardConfig == null)
    { 
        <text>考核目标未配置</text>
        return;
    }
    List<dynamic> list = new List<dynamic>();
    for (var dt = Model.BarModel.Start ?? DateTime.Now.AddMonths(-6); dt <= Model.BarModel.End; dt = dt.AddMonths(1))
    {
        var query = Model.SADayDatas.Where(o => o.AddTime.Year == dt.Year && o.AddTime.Month == dt.Month);
        var target = Model.MonthTarget.Where(o => o.AddTime.Year == dt.Year && o.AddTime.Month == dt.Month).FirstOrDefault();
        if (query == null || target == null)
        {
            list.Add(new { 
                时间=dt,
                考核目标 = Model.StandardConfig.流失客户回归率目标,
                隐形流失 =0
            });
        }else
        {
            list.Add(new
            {
                时间 = dt,
                考核目标 = Model.StandardConfig.流失客户回归率目标,
                //K6/E6
                //四个月未回厂客户总数/(C6+D6-H6))
                //本月新增四个月未回厂数/(上月末分管客户总数+本月A卡转C卡客户数-六个月未回厂客户总数)
                隐形流失 = (Model.MonthTarget.Sum(o => o.SYMFGKHZS_LastMonthManageCustomerNum - o.LGYWHCZS) + Model.SADayDatas.Sum(o => o.BYAKZCKKHS_ACard2CCard)) == 0 ? (decimal)0 : Math.Round((decimal)(Model.MonthTarget.Sum(o => o.LGYWHCZS) + Model.SADayDatas.Sum(o => o.XZLGYWHCS - o.LSKHCXHCS) / (Model.MonthTarget.Sum(o => o.SYMFGKHZS_LastMonthManageCustomerNum - o.LGYWHCZS) + Model.SADayDatas.Sum(o => o.BYAKZCKKHS_ACard2CCard))) * 100, 1, MidpointRounding.AwayFromZero)
            });
        }
    }

    S4Store.Models.Chart.Line ChartModel = new S4Store.Models.Chart.Line();
    ChartModel.Width = 600;
    ChartModel.Height = 400;
    ChartModel.Colors = new List<string> { S4Store.Models.Chart.Color.Green, S4Store.Models.Chart.Color.Blue };
    ChartModel.IsRate = false;
    ChartModel.YAxis = "百分率";

    foreach (var item in list.GroupBy(o => o.时间))
    {
        ChartModel.XAxis.Add(item.Key.Year + "-" + item.Key.Month);
    }
    S4Store.Models.Chart.ChartSeries series = new S4Store.Models.Chart.ChartSeries();
    series.Name = "考核目标";
    foreach (var item in list)
    {
        series.Data.Add(item.考核目标);
    }
    ChartModel.Serieses.Add(series);
    S4Store.Models.Chart.ChartSeries series1 = new S4Store.Models.Chart.ChartSeries();
    series1.Name = "隐形流失";
    foreach (var item in list)
    {
        series1.Data.Add(item.隐形流失);
    }
    ChartModel.Serieses.Add(series1);
}
<div style="height: 100%; overflow: auto">
    @Html.Action("ReportBar", "Common", Model.BarModel)
    <div style="position: relative; width: 100%">
        <div class="pagetitle">流失客户回归率对比</div>
        <table width="100%">
            <tr>
                <td style="width:50px">
                    &nbsp;
                </td>
                <td valign="top">
                    <p style=" padding-bottom:15px; font-size:15px;">@(Model.BarModel.Month.Month)月客户流失率</p>
                    <table class="reporttable" style="border-collapse: collapse;">
                        <thead>
                            <tr>
                                <th class="tb_header_lightbluebg">
                                    <div>
                                        客服专员姓名</div>
                                </th>
                                <th class="tb_header_lightbluebg">
                                    <div>
                                        客服专员职业资质
                                    </div>
                                </th>
                                <th class="tb_header_lightbluebg">
                                    <div>
                                        流失客户重新回厂数

                                    </div>
                                </th>
                                <th class="tb_header_lightbluebg">
                                    <div>
                                        流失客户回归率
                                    </div>
                                </th>
                            </tr>
                        </thead>
                        @foreach (var item in Model.CSDayDatas.GroupBy(o => o.EmployeeId))
                        {
                        <tr>
                            <td>@item.FirstOrDefault().Employee.Name</td>
                            <td>@(item.FirstOrDefault().Employee.WorkYear)年</td>
                            <td>@item.Sum(o=>o.LSKHCXHCS)</td>
                            <td @if(Model.StandardConfig.流失客户回归率目标>Model.GetLSKHHGL(item.Key,item.FirstOrDefault().AddTime.Year,item.FirstOrDefault().AddTime.Month))
                                {
                                    <text> style="background:#ff1100"</text>
                                }>@Model.GetLSKHHGL(item.Key,item.FirstOrDefault().AddTime.Year,item.FirstOrDefault().AddTime.Month)%</td>
                        </tr>
                        }
                        <tr>
                        <td colspan="2">
                            客服部平均数
                        </td>
                        <td>
                            @(Model.CSDayDatas.GroupBy(o => o.EmployeeId).Count()==0?0:Model.CSDayDatas.Sum(o => o.LSKHCXHCS)/Model.CSDayDatas.GroupBy(o => o.EmployeeId).Count())
                        </td>
                        <td>
                            @Model.GetAvgLSKHHGL_Back(Model.BarModel.Month.Year, Model.BarModel.Month.Month)%
                        </td>
                        </tr>
                        <tr>
                        <td colspan="2">
                            考核目标
                        </td>
                        <td>
                            
                        </td>
                        <td>
                        @if (Model.StandardConfig != null)
                        {
                            @Math.Round(Model.StandardConfig.流失客户回归率目标,1)<text>%</text>
                        }
                        </td>
                        </tr>
                    </table>
                </td>
                <td>
                    @Html.Partial("../Chart/Line", ChartModel)
                </td>
            </tr>
        </table>
    </div>
</div>